#include "view.h"

#include "render.h"
#include "texture.h"

#include "thumb.h"

#include <string.h>
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>

int ufoThumbAnimationShow(UFO_View view, int duration, int bHideAuto)
{
	THUMB *pThumb = ufoViewGetUserData(view);
	pThumb->bAnimationAutoHide = bHideAuto;
    
    if (duration > 0) {
        UFO_Animation_Value value = {Animation_alpha};
        value.alpha = 1.0f;

        ufoAnimationStop(pThumb->animation_alpha);
        ufoAnimationSetDelay(pThumb->animation_alpha, 0);
        ufoAnimationSetDuration(pThumb->animation_alpha, duration);
        ufoAnimationSetRepeatTime(pThumb->animation_alpha, 1);
        ufoAnimationSetAutoreverses(pThumb->animation_alpha, 0);
        ufoAnimationSetToValue(pThumb->animation_alpha, value);
        ufoAnimationPlay(pThumb->animation_alpha);
    }
    else {
        ufoAnimationStop(pThumb->animation_alpha);
        ufoViewSetAlpha(view, 1.0f);
        ufoViewSendMessage(view, MSG_ANIMATION_FINISH, pThumb->animation_alpha, 0);//### start hide animation
    }
	
	return 1;
}

int ufoThumbAnimationHide(UFO_View view)
{
	THUMB *pThumb = ufoViewGetUserData(view);
    int bPlay = ufoAnimationGetState(pThumb->animation_alpha);
    
    if (!bPlay) {
        
        UFO_Animation_Value value = {Animation_alpha};
        value.alpha = 0.0f;
        ufoAnimationStop(pThumb->animation_alpha);
        ufoAnimationSetDelay(pThumb->animation_alpha, 600);
        ufoAnimationSetDuration(pThumb->animation_alpha, 600);
        ufoAnimationSetRepeatTime(pThumb->animation_alpha, 1);
        ufoAnimationSetAutoreverses(pThumb->animation_alpha, 0);
        ufoAnimationSetToValue(pThumb->animation_alpha, value);
        ufoAnimationPlay(pThumb->animation_alpha);

        pThumb->bAnimationAutoHide = 0;
    }
    else {
        pThumb->bAnimationAutoHide = 1;
    }

	return 1;
}

int default_thumb_proc(UFO_View view, int id, void* wParam, void* lParam)
{
    THUMB *pThumb = ufoViewGetUserData(view);
	switch (id)
	{
	case MSG_TOUCH_DOWN:
		{
            if (pThumb) {
                ufoPoint *pt = wParam;
                pThumb->touch_point = *pt;
                pThumb->touched = 1;
            }	
		}break;

	case MSG_TOUCH_UP:
		{
            if (pThumb) {
                pThumb->touched = 0;
            }
		}break;

	case MSG_TOUCH_MOVE:
		{
			if (pThumb && pThumb->touched) {
				ufoPoint *pt = wParam;
				ufoRect rc = {0};
                ufoRect rcParent = {0};
                UFO_View parentView = ufoViewGetParent(view);
                ufoViewGetRect(parentView, &rcParent);
				ufoViewGetRect(view, &rc);
				ufoRectOffset(&rc, pt->x - pThumb->touch_point.x, pt->y - pThumb->touch_point.y);
                if (rc.top < 0) 
                    ufoRectOffset(&rc, 0, -rc.top);
                if (rc.bottom > rcParent.bottom-rcParent.top)
                    ufoRectOffset(&rc, 0, -(rc.bottom-(rcParent.bottom-rcParent.top)));
				if (rc.left < 0) 
					ufoRectOffset(&rc, -rc.left, 0);
				if (rc.right > rcParent.right-rcParent.left)
					ufoRectOffset(&rc, -(rc.right-(rcParent.right-rcParent.left)), 0);
				ufoViewSetRect(view, &rc);

				//### notify
				{
					UFO_NOTIFY_STRUCT notify = {0};
					notify.id = MSG_NOTIFY_THUMB_CHANGE;
					ufoViewSendMessage(parentView, MSG_NOTIFY, view, &notify);
				}
			}
		}break;

	case MSG_ANIMATION_FINISH:
		{
			if (pThumb->animation_alpha == (UFO_Animation)wParam) {
				if (pThumb->bAnimationAutoHide) {
					ufoThumbAnimationHide(view);
				}
			}
		}break;
            
    case MSG_CREATE:
        {
            THUMB *pThumb = malloc(sizeof(THUMB));
            memset(pThumb, 0, sizeof(THUMB));
            ufoViewSetUserData(view, pThumb);

			pThumb->animation_alpha = ufoAnimationCreate(view);
        }break;
	}

	return ufoViewDefaultProc(view, id, wParam, lParam);//### pass to default view proc
}